/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.action;

import java.util.List;

import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.model.equipment.EquipmentModel;
import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class DropObjectAction extends Action {
	private static final long serialVersionUID = 1L;

	private final Integer guidObjectIdx;

	private Long guidObject;

	public DropObjectAction(Long duration, Integer guidObjectIdx) {
		super(duration);
		this.guidObjectIdx = guidObjectIdx;
	}

	@Override
	protected Boolean init(List<Float> vals) {
		if (guidObjectIdx > vals.size()) {
			return false;
		}
		guidObject = vals.get(guidObjectIdx).longValue();
		return true;
	}

	@Override
	public void apply() {
		this.agentController.dropObject(guidObject);
	}

	@Override
	protected Boolean canDoAction() {
		IWorldModel world = this.agentController.getWorld();
		IEntityModel entity = this.agentController.getEntity();
		if (entity == null) {
			return false;
		}
		EquipmentModel equipment = world.getEquipment(guidObject);
		if (equipment == null) {
			return false;
		}

		if (equipment.getHolder() == null) {
			return false;
		}

		return entity.getGuid().equals(equipment.getHolder().getGuid());
	}

	@Override
	public Boolean isFinish() {
		IWorldModel world = this.agentController.getWorld();
		IEntityModel entity = this.agentController.getEntity();
		return !entity.equals(world.getEquipment(guidObject).getHolder());
	}

	@Override
	public Float generateEstimatedCost() {
		return 1f;
	}

	@Override
	public Float generateEstimatedWon() {
		return 1f;
	}

}
